package com.easybbs.utils;

import okhttp3.internal.Util;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.util.DigestUtils;

import java.util.Random;

public class StringTools {
    //判断是否为空
    public static Boolean isEmpty(String str) {
        if (null == str || "".equals(str.trim()) || "null".equals(str)) {
            return true;
        } else {
            return false;
        }
    }

    // 随机生成字母加数字随机数
    public static String getRandomString(int length) {
        return RandomStringUtils.random(length, true, true);
    }

    // 随机生成纯数字随机数
    public static String getRandomNum(int length) {
        return RandomStringUtils.random(length, false, true);
    }

    //MD5密码加密
    public static String encodeMd5(String sourceStr) {;
        return StringTools.isEmpty(sourceStr) ? null : DigestUtils.md5DigestAsHex(sourceStr.getBytes());
    }

    //获取文件后缀名
    public static String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".")); //获取最后一个.的索引位置
    }

    //获取文件名
    public static String getFileName(String fileName) {
        return fileName.substring(0, fileName.lastIndexOf("."));
    }

    //转义HTML内容
    public static String escapeHTML(String content) {
        if(StringTools.isEmpty(content)){
            return content;
        }
        content = content.replaceAll("<", "&lt;");
        content = content.replaceAll(" ", "&nbsp;");
        content = content.replaceAll("\n", "<br>");
        return content;
    }
}
